import {Injectable} from "@angular/core";
import {AlertController, Events, LoadingController, ModalController, ToastController} from "ionic-angular";
// import {websocket} from "./app.general";

@Injectable()
export class AppConfig {
  constructor(public toastCtrl: ToastController, public loadingCtrl: LoadingController, public modalCtrl: ModalController, public alertCtrl: AlertController, public events: Events) {
  }

  public loader:any;
  public modal:any;
  public websocket:any;
  public wsMsg:any;
  /**
   * 通用界面元素
   */
  public presentToast(mes: string, time?: number) {
    let toast = this.toastCtrl.create({
      message: mes,
      duration: time ? time : 3000,
      position: 'top'
    });
    toast.present();
  }

  presentLoading(mes: string, time?: number) {
    this.loader = this.loadingCtrl.create({
      content: mes,
      // duration: time ? time : 3000
    });
    this.loader.present();
  }

  presentAlert(title: string, mes: string, buttonMes: string) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: mes,
      buttons: [buttonMes]
    });
    alert.present();
  }

  presentModal(page: any, data: any) {
    this.modal = this.modalCtrl.create(page, {"data": data});
    this.modal.present();
  }

  colseModal(){
    this.modal.dismiss();
  }

  notSupport(){
    this.presentAlert('抱歉','APP内测中,该功能暂不开放','我知道了');
  }

  public connetWebSocket(){
    this.websocket=new WebSocket('ws://192.168.1.120:9099/');
    this.websocket.onopen=function (ev) {
      console.log('连接上websocket了!!!!');
      this.wsMsg='连接上websocket了';
    }
    this.websocket.onerror = function (event) {
      console.log(event);
      this.wsMsg=event;
    };
  }
}
